/*
 * @Author: wcx 1055127249@qq.com
 * @Date: 2025-01-09 17:01:07
 * @LastEditors: wcx 1055127249@qq.com
 * @LastEditTime: 2025-01-10 16:51:16
 * @FilePath: \my-project\src\views\CreateProcess\utils.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { getTextWidth } from '@/utils'

let errorList = []

export function nodeValidator(node, graph, msg = '请填写数据') {
  const item = errorList.find(i => i[0] === node.id)
  if (item) return msg
  const { x, y } = node.position
  const errorNode = {
    x,
    y: y + node.size.height,
    shape: 'polygon',
    width: getTextWidth(msg),
    height: 20,
    label: msg,
    data: {
      type: 'error',
      target: node.id
    },
    attrs: {
      body: {
        fill: 'transparent',
        stroke: 'transparent',
        points: '0,0 10,0 10,10 10,0',
      },
      label: {
        fontSize: 12,
        fill: '#f00'
      },
    }
  }
  const data = graph.addNode(errorNode)
  errorList.push([node.id, data])
  return msg
}
function nodeValidatorClear(node, graph) {
  const item = errorList.find(i => i[0] === node.id)
  if (item) {
    graph.removeNode(item[1].id)
    errorList = errorList.filter(i => i[0] !== node.id)
  }
}

export function processValidator(graph) {
  let errorInfo = ''
  const { cells } = graph.toJSON();
  const starts = cells.filter((i) => i.type === 'start')
  const ends = cells.filter((i) => i.type === 'end')
  if (starts.length !== 1) {
    errorInfo = starts.length === 0 ? '请添加流程开始节点' : '流程开始节点只能有一个'
  }
  if (ends.length !== 1) {
    errorInfo = ends.length === 0 ? '请添加流程结束节点' : '流程结束节点只能有一个'
  }
  if (errorInfo) return Promise.reject(new Error(errorInfo))
  for (let i = 0; i < cells.length; i++) {
    const node = cells[i]
    if (node.shape === 'bpmn-edge') continue
    const sourceLine = cells.filter(i => i?.source?.cell === node.id)
    const targetLine = cells.filter(i => i?.target?.cell === node.id)
    // console.log(node.shape, sourceLine, targetLine)
    if (node.type === 'start') {
      if (targetLine.length) {
        errorInfo = nodeValidator(node, graph, '开始节点连接有误')
      } else {
        nodeValidatorClear(node, graph)
      }
    } else if (node.type === 'end') {
      if (sourceLine.length) {
        errorInfo = nodeValidator(node, graph, '结束节点连接有误')
      } else {
        nodeValidatorClear(node, graph)
      }
    } else if (node.shape === 'activity') {
      if (sourceLine.length === 1 && targetLine.length === 1) {
        nodeValidatorClear(node, graph)
      } else {
        errorInfo = nodeValidator(node, graph, '任务节点连接有误')
      }
    } else if (node.shape === 'gateway') {
      if (sourceLine.length !== 0 && targetLine.length !== 0) {
        nodeValidatorClear(node, graph)
      } else {
        errorInfo = nodeValidator(node, graph, '网关节点连接有误')
      }
    }
    if (errorInfo) break;
  }
  if (errorInfo) return Promise.reject(new Error(errorInfo))
  return Promise.resolve()
  // console.log(start, end, gateway, activity)
}
function canvatCustomNodeData (data){
  const {label = '', error = '', ...obj} = data
  return {
    ...obj,
    attrs: {
      label: { text: label },
      error: { text: error }
    }
  }
}
export function addNoddd(graph) {
  const data = canvatCustomNodeData({
    shape: 'custom',
    label: '领导审批',
    position: {
      x: 440,
      y: 320
    }
  })
  graph.addNode(data)
}